<template>
    <el-dialog
      :visible.sync="visible"
      :title="dataForm.details ? '详情' :(!dataForm.id ? $t('add') : $t('update'))"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close()"
      :destroy-on-close="true"
      @opened="opened()"  
    >
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmitHandle()"
        :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'"
        label-position="right"
        :disabled="disabled"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="车牌类型" prop="numberplateColour">
              <SelectComponent
                ref="operatorType"
                dictType=""
                :options="options"
                v-model="dataForm.numberplateColour"
              >
              </SelectComponent>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收费标准" prop="chargeRmb">
              <el-input type="number" step="0.1" v-model="dataForm.chargeRmb" placeholder="收费标准"></el-input> 元/小时
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template slot="footer" v-if="!disabled">
        <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle()">{{
          $t("confirm")
        }}</el-button>
      </template>
    </el-dialog>
  </template>
  
  <script>
  import debounce from "lodash/debounce";
  import mixinRegionModule from "@/mixins/region-module";
  
  export default {
    mixins: [mixinRegionModule],
  
    data() {
      return {
        options: [
            { dictValue: "1", dictLabel: "蓝牌" },
            { dictValue: "2", dictLabel: "黄牌" },
            { dictValue: "3", dictLabel: "绿牌" },
            { dictValue: "4", dictLabel: "黄绿牌" },
            { dictValue: "5", dictLabel: "黑牌" },
            { dictValue: "6", dictLabel: "白牌" }
        ],
        visible: false,
        dataForm: {
          id: "",
          numberplate:"",
          numberplateColour: "",
          chargeRmb: ""
        },
      };
    },
    props: { disabled: Boolean },
    computed: {
      dataRule() {
        return {
            numberplateColour: [
            { required: true, message: "请选择车牌类型", trigger: "change" },
          ],
          chargeRmb: [
            { required: true, message: "请输入收费标准", trigger: "blur" },
          ]
        };
      },
    },
    methods: {
      opened(){
        let list = this.$parent.$parent.dataList;
        this.options.forEach((o,i)=>{
            this.$set(this.options[i],'disabled',list.find(l=>l.numberplate==o.dictLabel)?true:false);
        })
      },
      close() {
        this.dataForm = {
          id: "",
          numberplate:"",
          numberplateColour: "",
          chargeRmb: ""
        };
        this.$emit("changeDisabled");
      },
  
      init() {
        this.visible = true;
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields();
          if (this.dataForm.id) {
            this.getInfo();
          }
        });
      },
      // 获取信息
      getInfo() {
        this.$http
          .get("/travel/numberplateRmb/" + this.dataForm.id)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.dataForm = {
              ...this.dataForm,
              ...res.data,
            };
            this.$refs.operatorType.selectValue = this.dataForm.numberplateColour+'';
          })
          .catch(() => {});
      },
      // 表单提交
      dataFormSubmitHandle: debounce(
        function () {
          this.$refs["dataForm"].validate((valid) => {
            if (!valid) {
              return false;
            }

            this.dataForm.numberplate = this.options.find(o=>o.dictValue==this.dataForm.numberplateColour).dictLabel;
            this.$http[!this.dataForm.id ? "post" : "put"](
              !this.dataForm.id ? "/travel/numberplateRmb" : "/travel/numberplateRmb",
              this.dataForm
            )
              .then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg);
                }
                this.$message({
                  message: this.$t("prompt.success"),
                  type: "success",
                  duration: 500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              })
              .catch(() => {});
          });
        },
        1000,
        { leading: true, trailing: false }
      ),
    },
    watch: {
      "dataForm.numberplate": {
        deep: true,
        immediate: true,
        handler(newVal) {},
        selectValue: {
          deep: true,
          immediate: true,
          handler(newVal) {},
        },
      },
    },
  };
  </script>
  <style lang="scss" scoped >
  ::v-deep .el-input,.el-textarea  {
    width: auto
  }
  </style>
  